package com.rede.didiok.admin.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rede.didiok.admin.dao.WebNavbarDao;
import com.rede.didiok.admin.entity.WebNavbarEntity;
import com.rede.didiok.admin.service.WebNavbarService;
import com.rede.didiok.common.constant.conf.MessageConf;
import com.rede.didiok.common.constant.conf.SQLConf;
import com.rede.didiok.common.constant.conf.SysConf;
import com.rede.didiok.common.constant.global.Constants;
import com.rede.didiok.common.dto.admin.WebNavbarDto;
import com.rede.didiok.common.enums.StatusEnum;
import com.rede.didiok.common.utils.ResultUtil;
import com.rede.didiok.common.utils.StringUtils;


@Service("webNavbarService")
public class WebNavbarServiceImpl extends ServiceImpl<WebNavbarDao, WebNavbarEntity> implements WebNavbarService {

    @Autowired
    WebNavbarService webNavbarService;

    /***************************** admin:start **************************/
    // region  didiok-admin

    @Override
    public IPage<WebNavbarEntity> getPageList(WebNavbarDto webNavbarDto) {
        Map<String, Object> resultMap = new HashMap<>();
        QueryWrapper<WebNavbarEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(webNavbarDto.getKeyword()) && !StringUtils.isEmpty(webNavbarDto.getKeyword().trim())) {
            queryWrapper.like(SQLConf.NAME, webNavbarDto.getKeyword().trim());
        }
        if (webNavbarDto.getNavbarLevel() != 0) {
            queryWrapper.eq(SQLConf.MENU_LEVEL, webNavbarDto.getNavbarLevel());
        }
        Page<WebNavbarEntity> page = new Page<>();
        page.setCurrent(webNavbarDto.getCurrentPage());
        page.setSize(webNavbarDto.getPageSize());
        queryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);
        queryWrapper.orderByDesc(SQLConf.SORT);
        IPage<WebNavbarEntity> pageList = webNavbarService.page(page, queryWrapper);
        return pageList;
    }

    @Override
    public List<WebNavbarEntity> getAllList() {
        QueryWrapper<WebNavbarEntity> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq(SQLConf.NAVBAR_LEVEL, Constants.STR_ONE);
        queryWrapper.orderByDesc(SQLConf.SORT);
        queryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);

        List<WebNavbarEntity> allList = webNavbarService.list(queryWrapper);

        // 找出所有 navbar_level = 1 的一级导航菜单项的ID
        List<WebNavbarEntity> list  = allList.stream().filter(e -> Constants.INTEGER_ONE.equals(e.getNavbarLevel())).collect(Collectors.toList());
        List<String> ids = list.stream().map(WebNavbarEntity::getUid).collect(Collectors.toList());

        // 根据 ids 查询对应的二级导航
        Collection<WebNavbarEntity> childList = allList.stream().filter(e -> ids.contains(e.getParentUid())).collect(Collectors.toList());

        // 给一级导航栏设置二级导航栏
        for (WebNavbarEntity parentItem : list) {
            List<WebNavbarEntity> tempList = new ArrayList<>();
            for (WebNavbarEntity item : childList) {
                if (item.getParentUid().equals(parentItem.getUid())) {
                    tempList.add(item);
                }
            }
            Collections.sort(tempList);
            parentItem.setChildWebNavbar(tempList);
        }
        return list;
    }

    @Override
    public String addWebNavbar(WebNavbarDto webNavbarDto) {
        //如果是一级菜单，将父ID清空
        if (webNavbarDto.getNavbarLevel() == 1) {
            webNavbarDto.setParentUid("");
        }
        WebNavbarEntity webNavbar = new WebNavbarEntity();
        // 插入数据【使用Spring工具类提供的深拷贝】
        BeanUtils.copyProperties(webNavbarDto, webNavbar, SysConf.STATUS);
        webNavbar.insert();
        return ResultUtil.successWithMessage(MessageConf.INSERT_SUCCESS);
    }

    @Override
    public String editWebNavbar(WebNavbarDto webNavbarDto) {
        //如果是一级菜单，将父ID清空
        if (webNavbarDto.getNavbarLevel() == 1) {
            webNavbarDto.setParentUid("");
        }
        WebNavbarEntity webNavbar = webNavbarService.getById(webNavbarDto.getUid());
        // 插入数据【使用Spring工具类提供的深拷贝】
        BeanUtils.copyProperties(webNavbarDto, webNavbar, SysConf.STATUS, SysConf.UID);
        webNavbar.setUpdateTime(new Date());
        webNavbarService.updateById(webNavbar);
        return ResultUtil.successWithMessage(MessageConf.UPDATE_SUCCESS);
    }

    @Override
    public String deleteWebNavbar(WebNavbarDto webNavbarDto) {
        QueryWrapper<WebNavbarEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SQLConf.STATUS, StatusEnum.ENABLE);
        queryWrapper.in(SQLConf.PARENT_UID, webNavbarDto.getUid());
        Integer menuCount = webNavbarService.count(queryWrapper);
        if (menuCount > 0) {
            return ResultUtil.errorWithMessage(MessageConf.CHILDREN_MENU_UNDER_THIS_MENU);
        }
        WebNavbarEntity webNavbar = webNavbarService.getById(webNavbarDto.getUid());
        webNavbar.setStatus(StatusEnum.DISABLED);
        webNavbar.setUpdateTime(new Date());
        webNavbar.updateById();
        return ResultUtil.successWithMessage(MessageConf.DELETE_SUCCESS);
    }

    // endregion
    /***************************** admin:end **************************/



    /***************************** web:start **************************/
    // region didiok-web

    // endregion
    /***************************** web:end **************************/

}